package indi.mozping.mozping;

import java.sql.*;
import java.util.Random;

/**
 * @author by mozping
 * @Classname Refer
 * @Description TODO
 * @Date 2020/4/9 17:21
 */
public class ReadWriteData {

    static {
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {

        //readData();
        //writeData();

    }


    private static void writeData() {

        Statement stmt;
        Connection con;
        String ItemID = getItemID(10);
        String ItemName = getItemName(10);
        float Price = (float) 0.1;
        try {
            String sql = "upsert into items (ItemID,ItemName,Price) values (?,?,?)";
            con = DriverManager.getConnection(Config.CONN_URL);
            stmt = con.createStatement();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            System.out.println("content success....");

//            stmt.executeUpdate("create table if not exists items ( ItemID varchar not null primary key,ItemName VARCHAR NULL,Price VARCHAR NULL) ");
            for (int i = 0; i < 1; i++) {
                //stmt.executeUpdate("upsert into items values ('" + getItemID(10) + "','" + getItemName(10) + "','" + Price++ + "')");
                stmt.executeUpdate("upsert into person values ('" + getItemID(10) + "','" + getItemName(10) + "')");
            }
            con.commit();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String getItemID(int n) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            String str = random.nextInt(2) % 2 == 0 ? "num" : "char";
            if ("char".equalsIgnoreCase(str)) {
                // ������ĸ
                int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
                // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                val += (char) (nextInt + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(str)) {
                // ��������
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * ����ItemName�������
     *
     * @param length
     * @return
     */
    public static String getItemName(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
